package edu.wayne.cs.csc4110.hw6.framework.lobby;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.util.Observable;

import javax.swing.JFrame;
import javax.swing.JPanel;

import edu.wayne.cs.csc4110.hw6.framework.gui.lobby.ChatArea;
import edu.wayne.cs.csc4110.hw6.framework.gui.lobby.MenuNavigation;
import edu.wayne.cs.csc4110.hw6.framework.gui.lobby.SendMessagesPanel;
import edu.wayne.cs.csc4110.hw6.framework.message.LobbyMessage;
import edu.wayne.cs.csc4110.hw6.framework.plugin.GamePluginLoop;
import edu.wayne.cs.csc4110.hw6.interfaces.lobby.LobbyClient;
import edu.wayne.cs.csc4110.hw6.interfaces.user.User;

public class GameLobbyClient extends JPanel implements LobbyClient {

	private User activeUser;
	private ChatArea chatArea;
	private SendMessagesPanel sendMsgPanel;
	private MenuNavigation navigation;
	private JPanel container;

	private JFrame topFrame;

	public GameLobbyClient(final User user, GamePluginLoop gamePluginLoop,
			JFrame topFrame) {
		setTopFrame(topFrame);
		this.activeUser = user;
		this.setLayout(new FlowLayout(FlowLayout.LEFT));

		container = new JPanel();
		container.setLayout(new BorderLayout());
		chatArea = new ChatArea(user);
		sendMsgPanel = new SendMessagesPanel(this);
		navigation = new MenuNavigation(user, gamePluginLoop, topFrame);
		container.add(navigation, BorderLayout.NORTH);
		container.add(chatArea, BorderLayout.CENTER);
		container.add(sendMsgPanel, BorderLayout.SOUTH);
		this.add(container);

	}

	@Override
	public void update(Observable o, Object arg) {
		if (arg instanceof LobbyMessage) {
			chatArea.appendMessage(((LobbyMessage) arg));
		}
		if (arg instanceof User) {
			chatArea.updateUserList((User) arg);
		}

	}

	@Override
	public void draw(Graphics gfx) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendMessage(LobbyMessage msg) {
		GameLobbyServer.getInstance().sendMessage(msg);

	}

	public User getUser() {
		return activeUser;
	}

	public JFrame getTopFrame() {
		return topFrame;
	}

	public void setTopFrame(JFrame topFrame) {
		this.topFrame = topFrame;
	}

}
